"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 主机或备机数据库运行日志操作日志权限异常（gaussdb_log权限异常），主停服，备升主
Create At   : 2020/07/20
@zou_jialiang0505328114
Description :
    步骤 1.启动主备数据库，判断数据库状态；
    步骤 2.修改主备数据库gaussdb_log权限；
    步骤 3.备升主；
Expect      :    
History     :
    modified: 2021/01/22  modified by @wan005,优化脚本
"""
import os
import unittest
import _thread
import time
from yat.test import macro
from yat.test import Node
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Deletaduit(unittest.TestCase):
    commonshpri = CommonSH('PrimaryDbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')
    standby_1_sh = CommonSH('Standby1DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.constant = Constant()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case152.py start------------')
        self.rootNode = Node()
        self.com = Common()
        self.dbUserNode1 = Node(node='PrimaryDbUser')
        self.dbUserNode = Node(node='PrimaryRoot')
        self.dbUserNode2 = Node(node='Standby1Root')
        self.dbUserNode3 = Node(node='Standby1DbUser')
        self.startCmd = f'source {macro.DB_ENV_PATH};gs_om -t start'
        self.stopCmd = f'source {macro.DB_ENV_PATH};gs_om -t stop'
        self.sourcecmd = f'source {macro.DB_ENV_PATH}'
        self.statusCmd = f'source {macro.DB_ENV_PATH};gs_om -t status --detail'
        self.switchovercmd = f'{self.sourcecmd};gs_ctl switchover -D {macro.DB_INSTANCE_PATH};gs_om -t refreshconf;'

    def test_startdb(self):
        self.glogpath = macro.DB_INSTANCE_PATH.split('/dn1')[0] + '/' + 'gaussdb_log'
        # 修改主数据库gaussdb_log权限；
        self.log.info('chmod 000 ' + self.glogpath)
        self.dbUserNode.sh('chmod 000 ' + self.glogpath)
        # 备升主；
        msg1 = self.dbUserNode3.sh(self.switchovercmd).result()
        self.assertIn('switchover completed', msg1)
        # 恢复数据库
        self.log.info('chmod 750 ' + self.glogpath)
        self.dbUserNode.sh('chmod 750 ' + self.glogpath)
        result = self.dbUserNode1.sh(self.stopCmd).result()
        self.log.info(result)
        time.sleep(10)

        result = self.dbUserNode1.sh(self.startCmd).result()
        self.log.info(result)
        time.sleep(10)

        result = self.commonshpri.buildStandby()
        self.log.info(result)
        self.assertIn(self.constant.REBUILD_SUCCESS_MSG, result)
        time.sleep(10)

        self.dbUserNode1.sh(self.switchovercmd).result()
        time.sleep(10)

        # 修改备数据库gaussdb_log权限；
        self.log.info('chmod 000 ' + self.glogpath)
        self.dbUserNode2.sh('chmod 000 ' + self.glogpath)
        # 备升主；
        msg3 = self.dbUserNode3.sh(self.switchovercmd).result()
        self.log.info(msg3)
        self.assertIn('Permission denied', msg3)
        self.dbUserNode2.sh('chmod 750 ' + self.glogpath)
        # 恢复数据库

    def tearDown(self):
        # 恢复数据库
        msg1 = self.com.kill_pid(self.dbUserNode, 9)
        self.assertEqual(msg1, '')
        msg1 = self.com.kill_pid(self.dbUserNode2, 9)
        self.assertEqual(msg1, '')
        self.dbUserNode3.sh(self.startCmd)
        time.sleep(10)
        result = self.standby_1_sh.buildStandby()
        self.log.info(result)
        time.sleep(10)
        status = self.dbUserNode1.sh(self.statusCmd).result()
        print(status)
        self.dbUserNode1.sh(self.switchovercmd).result()
        status = self.dbUserNode1.sh(self.statusCmd).result()
        if not status:
            raise Exception("db status is not true, please check!")
        else:
            pass
        time.sleep(10)
        self.log.info(
            '------------------------------------reliability_dbsys152.py finish------------------------------------')
